package cn.react.filereceive;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.provider.MediaStore;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.File;

class ShareFileInfo{
    String mType="";
    Uri uri = null;

    public ShareFileInfo(String type, Uri data) {
        mType = type;
        uri = data;
    }
}

class FileInfo{
    String name="";
    String size="";
    public FileInfo(String _name,String _size)
    {
        name = _name;
        size = _size;
    }
}
@SuppressWarnings("unused")
public class FileReceiveModule extends ReactContextBaseJavaModule{
    private ReactApplicationContext reactContext;
    private DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter;
    private static FileReceiveModule sModule =null;
    private  static ShareFileInfo sShareInfo = null;
    MediaPlayer mMediaPlayer=null;
    FileReceiveModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.onCreate(reactContext);
    }
    public void onCreate(ReactApplicationContext reactContext)
    {
        this.reactContext = reactContext;
        FileReceiveModule.sModule = this;
    }
    @Override
    public String getName() {
        return "FileReceive";
    }
    @ReactMethod
    public void playRingtone()
    {
        AudioManager am = (AudioManager)this.reactContext.getSystemService((Context.AUDIO_SERVICE)) ;
        switch (am.getRingerMode())
        {
            case AudioManager.RINGER_MODE_NORMAL:
                Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone rt = RingtoneManager.getRingtone(this.reactContext, uri);
                rt.play();

                break;
            case AudioManager.RINGER_MODE_SILENT:
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                Vibrator v = (Vibrator)this.reactContext.getSystemService(Context.VIBRATOR_SERVICE);
                long[] patter = {0,500,50};
                v.vibrate(patter,-1);
//                v.cancel();
                break;
        }
    }
    @ReactMethod
    public void checkFile() {
        eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
        if(FileReceiveModule.sShareInfo!=null)
        {
            ShareFileInfo info  = FileReceiveModule.sShareInfo;
            handleUri(info,"0");
            FileReceiveModule.sShareInfo = null;
        }
    }

    /**
     *
     */
    public static FileInfo getFieInfoFromURI(Context context,Uri contentURI) {
        String result =null;
        Cursor cursor = null;
        String name = "";
        String size = "";
        try {
            String mimeType =  context.getContentResolver().getType(contentURI);
            String[] proj = {MediaStore.Images.Media.DATA,MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.SIZE};
            context.getContentResolver().openFileDescriptor(contentURI,"r");
            cursor = context.getContentResolver().query(contentURI, proj, null, null, null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        if (cursor == null) {
          //  result = contentURI.getPath();
            return null;
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
            if(idx>-1)
            {

                name = cursor.getString(idx);
            }
            idx  = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
            if(idx>-1)
            {
                size = cursor.getString(idx);
            }
            cursor.close();
        }
        return new FileInfo(name,size);
    }

    private void handleUri(ShareFileInfo info,String code) {

        Uri uri = info.uri;
        if(uri.getScheme().equals("content"))
        {
            FileInfo fileinfo = getFieInfoFromURI(reactContext,uri);
            WritableMap map = Arguments.createMap();
            map.putString("filePath",uri.toString());
            map.putString("code",code);
            map.putString("mimeType",info.mType);
            map.putString("size",fileinfo.size);
            map.putString("name",fileinfo.name);
            this.eventEmitter.emit(this.getName(),map);
        }
        else if(uri.getScheme().equals("file")) {
            File file = new File(uri.getPath());
            if (!file.exists()){

                return;
            }
            WritableMap map = Arguments.createMap();
            map.putString("filePath", uri.toString());
            map.putString("code", code);
            map.putString("mimeType",info.mType);
            String size = "" + file.length();
            map.putString("size","" + file.length());
            map.putString("name",file.getName());

            this.eventEmitter.emit(this.getName(), map);
        }
    }

    public static void handleIntent(Intent intent,int code)
    {
        if(intent.getType()!=null&&intent.getData()!=null)
        {
            FileReceiveModule.sShareInfo  =  new ShareFileInfo(intent.getType(),intent.getData());
            if(code == 1)
            {
                if(FileReceiveModule.sModule!=null)
                {
                    Uri  u = intent.getData();
                    FileReceiveModule.sModule.handleUri(FileReceiveModule.sShareInfo,"1");
                    FileReceiveModule.sShareInfo = null;
                }
            }
            else if(code == 0)
            {

            }
        }
    }


}
